{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn import tree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Importing the dataset\n",
    "\n",
    "df_train=pd.read_csv('train.csv')\n",
    "\n",
    "df_new=df_train.drop(labels=['Ticket','Cabin','Embarked','Name'], axis=1)\n",
    "#print(df_new.head(4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_new1=df_new.fillna(method='ffill',axis=0)\n",
    "#print(df_new1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "1",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-6-d48ecc078b1d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#print(df_new1.Sex)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mgender\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'male'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'female'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mdf_new1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mgender\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mdf_new1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-6-d48ecc078b1d>\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#print(df_new1.Sex)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mgender\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;34m'male'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'female'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m2\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[0mdf_new1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSex\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mgender\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mdf_new1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSex\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 1"
     ]
    }
   ],
   "source": [
    "#print(df_new1.Sex)\n",
    "gender = {'male': 1,'female': 2}\n",
    "df_new1.Sex=[gender[item] for item in df_new1.Sex]\n",
    "\n",
    "\n",
    "print(df_new1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Making the data worthy of the Decision tree\n",
    "\n",
    "train_label=df_new1.Survived\n",
    "#print(train_label)\n",
    "\n",
    "#conti_features=['Age','SibSp','Parch','Fare','Pclass']\n",
    "#Categorical_features=['Sex']\n",
    "\n",
    "#Data conversion\n",
    "#continuous_features=[tf.feature_column.numeric_column(k) for k in conti_features]\n",
    "\n",
    "#print(continuous_features)\n",
    "#Converting categorical features\n",
    "\n",
    "\n",
    "#print(train_data.head(4))\n",
    "#Categorical_features=['Sex']\n",
    "\n",
    "\n",
    "#feature_columns=continuous_features+Categorical_features"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_train=df_new1[['Pclass','Sex','Age','SibSp','Parch','Fare']]\n",
    "#print(df_train)\n",
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_train, train_label, test_size = 0.3, random_state = 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69     0\n",
      "85     1\n",
      "794    0\n",
      "161    1\n",
      "815    0\n",
      "516    1\n",
      "299    1\n",
      "454    0\n",
      "59     0\n",
      "879    1\n",
      "42     0\n",
      "499    0\n",
      "235    0\n",
      "254    0\n",
      "345    1\n",
      "104    0\n",
      "315    1\n",
      "872    0\n",
      "3      1\n",
      "804    1\n",
      "224    1\n",
      "771    0\n",
      "538    0\n",
      "248    1\n",
      "31     1\n",
      "267    1\n",
      "543    1\n",
      "684    0\n",
      "607    1\n",
      "748    0\n",
      "      ..\n",
      "612    1\n",
      "723    0\n",
      "757    0\n",
      "141    1\n",
      "415    0\n",
      "667    0\n",
      "386    0\n",
      "86     0\n",
      "93     0\n",
      "649    1\n",
      "507    1\n",
      "570    1\n",
      "316    1\n",
      "228    0\n",
      "655    0\n",
      "280    0\n",
      "752    0\n",
      "802    1\n",
      "526    1\n",
      "738    0\n",
      "578    0\n",
      "53     1\n",
      "866    1\n",
      "350    0\n",
      "79     1\n",
      "855    1\n",
      "871    1\n",
      "835    1\n",
      "792    0\n",
      "520    1\n",
      "Name: Survived, Length: 623, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Defining the input function\n",
    "\n",
    "def get_input_fn(data_set, num_epochs=None, n_batch = 128, shuffle=True):\n",
    "    return tf.estimator.inputs.pandas_input_fn(\n",
    "       x=pd.DataFrame({k: data_set[k].values for k in feature_columns}),\n",
    "       y = pd.Series(data_set[LABEL].values),\n",
    "       batch_size=n_batch,   \n",
    "       num_epochs=num_epochs,\n",
    "       shuffle=shuffle)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=3,\n",
       "            max_features=None, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, presort=False, random_state=100,\n",
       "            splitter='best')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Training the classifier\n",
    "classifier=DecisionTreeClassifier(criterion=\"gini\",random_state=100,max_depth=3)\n",
    "classifier.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test=pd.read_csv('test.csv')\n",
    "df_test=df_test.drop(labels=['Ticket','Cabin','Embarked','Name'], axis=1)\n",
    "gender = {'male': 1,'female': 2}\n",
    "df_test.Sex=[gender[item] for item in df_test.Sex]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     PassengerId  Pclass  Sex   Age  SibSp  Parch      Fare\n",
      "0            892       3    1  34.5      0      0    7.8292\n",
      "1            893       3    2  47.0      1      0    7.0000\n",
      "2            894       2    1  62.0      0      0    9.6875\n",
      "3            895       3    1  27.0      0      0    8.6625\n",
      "4            896       3    2  22.0      1      1   12.2875\n",
      "5            897       3    1  14.0      0      0    9.2250\n",
      "6            898       3    2  30.0      0      0    7.6292\n",
      "7            899       2    1  26.0      1      1   29.0000\n",
      "8            900       3    2  18.0      0      0    7.2292\n",
      "9            901       3    1  21.0      2      0   24.1500\n",
      "10           902       3    1  21.0      0      0    7.8958\n",
      "11           903       1    1  46.0      0      0   26.0000\n",
      "12           904       1    2  23.0      1      0   82.2667\n",
      "13           905       2    1  63.0      1      0   26.0000\n",
      "14           906       1    2  47.0      1      0   61.1750\n",
      "15           907       2    2  24.0      1      0   27.7208\n",
      "16           908       2    1  35.0      0      0   12.3500\n",
      "17           909       3    1  21.0      0      0    7.2250\n",
      "18           910       3    2  27.0      1      0    7.9250\n",
      "19           911       3    2  45.0      0      0    7.2250\n",
      "20           912       1    1  55.0      1      0   59.4000\n",
      "21           913       3    1   9.0      0      1    3.1708\n",
      "22           914       1    2   9.0      0      0   31.6833\n",
      "23           915       1    1  21.0      0      1   61.3792\n",
      "24           916       1    2  48.0      1      3  262.3750\n",
      "25           917       3    1  50.0      1      0   14.5000\n",
      "26           918       1    2  22.0      0      1   61.9792\n",
      "27           919       3    1  22.5      0      0    7.2250\n",
      "28           920       1    1  41.0      0      0   30.5000\n",
      "29           921       3    1  41.0      2      0   21.6792\n",
      "..           ...     ...  ...   ...    ...    ...       ...\n",
      "388         1280       3    1  21.0      0      0    7.7500\n",
      "389         1281       3    1   6.0      3      1   21.0750\n",
      "390         1282       1    1  23.0      0      0   93.5000\n",
      "391         1283       1    2  51.0      0      1   39.4000\n",
      "392         1284       3    1  13.0      0      2   20.2500\n",
      "393         1285       2    1  47.0      0      0   10.5000\n",
      "394         1286       3    1  29.0      3      1   22.0250\n",
      "395         1287       1    2  18.0      1      0   60.0000\n",
      "396         1288       3    1  24.0      0      0    7.2500\n",
      "397         1289       1    2  48.0      1      1   79.2000\n",
      "398         1290       3    1  22.0      0      0    7.7750\n",
      "399         1291       3    1  31.0      0      0    7.7333\n",
      "400         1292       1    2  30.0      0      0  164.8667\n",
      "401         1293       2    1  38.0      1      0   21.0000\n",
      "402         1294       1    2  22.0      0      1   59.4000\n",
      "403         1295       1    1  17.0      0      0   47.1000\n",
      "404         1296       1    1  43.0      1      0   27.7208\n",
      "405         1297       2    1  20.0      0      0   13.8625\n",
      "406         1298       2    1  23.0      1      0   10.5000\n",
      "407         1299       1    1  50.0      1      1  211.5000\n",
      "408         1300       3    2  50.0      0      0    7.7208\n",
      "409         1301       3    2   3.0      1      1   13.7750\n",
      "410         1302       3    2   3.0      0      0    7.7500\n",
      "411         1303       1    2  37.0      1      0   90.0000\n",
      "412         1304       3    2  28.0      0      0    7.7750\n",
      "413         1305       3    1  28.0      0      0    8.0500\n",
      "414         1306       1    2  39.0      0      0  108.9000\n",
      "415         1307       3    1  38.5      0      0    7.2500\n",
      "416         1308       3    1  38.5      0      0    8.0500\n",
      "417         1309       3    1  38.5      1      1   22.3583\n",
      "\n",
      "[418 rows x 7 columns]\n"
     ]
    }
   ],
   "source": [
    "df_test=df_test.fillna(method='ffill',axis=0)\n",
    "print(df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred=classifier.predict(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 0 1 0 0 0 0 1 0 1 0 0 1 1 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 0 1 0 0 1 0 1\n",
      " 1 1 0 1 0 0 1 0 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 0 1\n",
      " 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 0 1 1 0 0 1 1 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0\n",
      " 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 1 1 0 0 0 0 1 1\n",
      " 0 1 1 0 0 1 0 1 0 1 1 0 1 1 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 0 1 0 0 0 1 0 0\n",
      " 0 0 0 1 1 1 0 1 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1\n",
      " 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0\n",
      " 0 1 1 0 1 1 0 1 0]\n"
     ]
    }
   ],
   "source": [
    "print(y_pred)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "78.73134328358209\n"
     ]
    }
   ],
   "source": [
    "print(accuracy_score(y_test,y_pred)*100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_test1=df_test[['Pclass','Sex','Age','SibSp','Parch','Fare']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 0 0 1 0]\n"
     ]
    }
   ],
   "source": [
    "y_pred_test=classifier.predict(df_test1)\n",
    "print(y_pred_test[0:6])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(418,)\n"
     ]
    }
   ],
   "source": [
    "y_"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
